package configs

import (
	"github.com/spf13/viper"
	"time"
)

type Config struct {
	App      AppConfig
	Server   ServerConfig
	Database DatabaseConfig
	Logger   LoggerConfig
	Notifier NotifierConfig
}

type AppConfig struct {
	Env  string
	Name string
}

type ServerConfig struct {
	Port int
	Mode string
}

type DatabaseConfig struct {
	DSN             string
	MaxIdleConns    int
	MaxOpenConns    int
	ConnMaxLifetime time.Duration
}

type LoggerConfig struct {
	Level      string
	Filename   string
	MaxSize    int
	MaxBackups int
	MaxAge     int
	Compress   bool
}

type NotifierConfig struct {
	WechatWork WechatWorkConfig
	Timeout    time.Duration
}

type WechatWorkConfig struct {
	Enabled bool
	Webhook string
}

func Load(path string) (*Config, error) {
	viper.SetConfigFile(path)

	if err := viper.ReadInConfig(); err != nil {
		return nil, err
	}

	var cfg Config
	if err := viper.Unmarshal(&cfg); err != nil {
		return nil, err
	}

	return &cfg, nil
}
